package io.bdmc.common.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDateTime;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PDFReport {
    private static final Logger logger = LoggerFactory.getLogger((PDFReport.class));
    public static void generatePDF(String[] args,String filename) {
        try {
            // 1.新建document对象
            Document document = new Document();

            // 2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
            // 创建 PdfWriter 对象 第一个参数是对文档对象的引用，第二个参数是文件的实际名称，在该名称中还会给出其输出路径。
            File path = new File(Constant.EXPORTPATH);
            if (!path.exists()) {
                path.mkdir();
            }
            File f = new File(path.getAbsolutePath() + "/excel/", filename);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
            writer.flush();
            writer.close();
            // 3.打开文档
            document.open();
            // 4.添加一个内容段落
            BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //Font headfont=new Font(bfChinese,10,Font.BOLD);
            Font keyfont=new Font(bfChinese,20,Font.BOLD);
            Font textfont=new Font(bfChinese,14,Font.NORMAL);
            document.addTitle(args[0] + "消防安全管理月报");
            Paragraph title = new Paragraph(args[0] +"消防安全管理月报",keyfont);
            title.setSpacingAfter(30);
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);
            Paragraph p1 = new Paragraph("一、设备运行状况",keyfont);
            p1.setSpacingAfter(10);
            document.add(p1);
            String spaceStr = "      ";
            document.add(new Paragraph(spaceStr+args[1],textfont));
            document.add(new Paragraph(spaceStr+args[2],textfont));

            Paragraph p2 = new Paragraph("二、火警警报分析",keyfont);
            p2.setSpacingBefore(15);
            p2.setSpacingAfter(10);
            document.add(p2);
            document.add(new Paragraph(spaceStr+args[3],textfont));
            document.add(new Paragraph(spaceStr+args[4],textfont));
            document.add(new Paragraph(spaceStr+args[5],textfont));

            Paragraph p3 = new Paragraph("三、隐患警报分析",keyfont);
            p3.setSpacingBefore(15);
            p3.setSpacingAfter(10);
            document.add(p3);

            document.add(new Paragraph(spaceStr+args[6],textfont));
            document.add(new Paragraph(spaceStr+args[7],textfont));

            Paragraph p4 = new Paragraph("智慧消防云平台",textfont);
            p4.setIndentationLeft(350);
            p4.setSpacingBefore(100);
            document.add(p4);
            LocalDateTime now = LocalDateTime.now();
            Paragraph p5 = new Paragraph(now.getYear()+"年"+now.getMonth().getValue()+"月"+now.getDayOfMonth()+"日",textfont);
            p5.setIndentationLeft(355);
            document.add(p5);
            // 5.关闭文档
            document.close();
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

    }

}